import system from '@/api/system'
import {SET_PRODUCT_TYPE, SET_PRODUCT_UNITS, SET_SERVICE_TYPE, SET_PAYMENT_TYPE, SET_VISIT_TYPE} from '../mutations'

const common = {
  namespaced: true,
  state: {
    productTypes: [],
    serviceTypes: [],
    paymentTypes: [],
    visitTypes: [],
    productUnits: ['件', '个', '套', '把', '盒']
  },
  mutations: {
    [SET_PRODUCT_TYPE]: (state, data) => {
      state.productTypes = data
    },
    [SET_PRODUCT_UNITS]: (state, data) => {
      state.productUnits = data
    },
    [SET_SERVICE_TYPE]: (state, data) => {
      state.serviceTypes = data
    },
    [SET_PAYMENT_TYPE]: (state, data) => {
      state.paymentTypes = data
    },
    [SET_VISIT_TYPE]: (state, data) => {
      state.visitTypes = data
    }
  },
  actions: {
    RefreshDic ({commit, dispatch}, dicType) {
      if (dicType && dicType !== null) {
        if (dicType === 'productType') {
          dispatch('GetProductType', dicType)
        } else if (dicType === 'serviceType') {
          dispatch('GetServiceType', dicType)
        } else if (dicType === 'paymentType') {
          dispatch('GetPaymentType', dicType)
        } else if (dicType === 'visitType') {
          dispatch('GetVisitType', dicType)
        }
      } else {
        dispatch('GetProductType', 'productType')
        dispatch('GetServiceType', 'serviceType')
        dispatch('GetPaymentType', 'paymentType')
        dispatch('GetVisitType', 'visitType')
      }
    },
    /**
     * 获取产品类型信息
     * @param commit
     * @param dispatch
     * @constructor
     */
    GetProductType ({commit, dispatch}) {
      system.getDictListByBizTypeAPI('productType').then(res => {
        commit(SET_PRODUCT_TYPE, res.data || [])
      })
    },
    /**
     * 获取服务类型信息
     * @param commit
     * @param dispatch
     * @constructor
     */
    GetServiceType ({commit, dispatch}) {
      system.getDictListByBizTypeAPI('serviceType').then(res => {
        commit(SET_SERVICE_TYPE, res.data || [])
      })
    },
    /**
     * 获取支付类型信息
     * @param commit
     * @param dispatch
     * @constructor
     */
    GetPaymentType ({commit, dispatch}) {
      system.getDictListByBizTypeAPI('paymentType').then(res => {
        commit(SET_PAYMENT_TYPE, res.data || [])
      })
    },
    /**
     * 获取访问类型信息
     * @param commit
     * @param dispatch
     * @constructor
     */
    GetVisitType ({commit, dispatch}) {
      system.getDictListByBizTypeAPI('visitType').then(res => {
        commit(SET_VISIT_TYPE, res.data || [])
      })
    },
    GetProductUnits (commit) {
      commit(SET_PRODUCT_UNITS, ['件', '个', '套', '把', '盒'])
    }
  }
}
export default common
